#include "..\include\Grid.h"

Grid::Grid(int a, int b) {
	height = a;
	width = b;

	//Presets of alive cells. Change these to whatever you wish.
	grid[0][1] = true;
	grid[1][2] = true;
	grid[2][0] = true;
	grid[2][1] = true;
	grid[2][2] = true;

}

/**
 Checks how many surrounding cells are alive for a cell and changes it depending on the game of life rules.
 The changes are stored in a temporary 2D array. Then, it is copied to the main grid array.
**/
void Grid::nextFrame(bool fredkin) {

	int numSurrounding = 0;
	bool tempGrid [height][width];
	int R = height;
	int C = width;

	for (int i = 0; i < height ; i++)
	{
		for (int j = 0; j < width ; j++)
		{
			if ( grid[(i+1+R)%R][j] == true )
			{
				numSurrounding++;
			}
			if ( grid[(i-1+R)%R][j] == true )
			{
				numSurrounding++;
			}
			if ( grid[i][(j+1+C)%C] == true )
			{
				numSurrounding++;
			}
			if ( grid[i][(j-1+C)%C] == true )
			{
				numSurrounding++;
			}
			if ( grid[(i+1+R)%R][(j+1+C)%C] == true )
			{
				numSurrounding++;
			}
			if ( grid[(i+1+R)%R][(j-1+C)%C] == true )
			{
				numSurrounding++;
			}
			if ( grid[(i-1+R)%R][(j+1+C)%C] == true )
			{
				numSurrounding++;
			}
			if ( grid[(i-1+R)%R][(j-1+C)%C] == true )
			{
				numSurrounding++;
			}
			/*if(i==0){
			   if(
			}*/

         if(fredkin == false){
            if (numSurrounding < 2 || numSurrounding > 3)
            {
               tempGrid[i][j] = false;
            }
            else if (numSurrounding == 2)
            {
               tempGrid[i][j] = grid[i][j];
            }
            else if (numSurrounding == 3)
            {
               tempGrid[i][j] = true;
            }
         } else {
            if(numSurrounding%2 == 0){
               tempGrid[i][j] = false;
            }
            if(numSurrounding%2 == 1){
               tempGrid[i][j] = true;
            }
         }

			numSurrounding = 0;

		}
	}

	for (int i = 0 ; i < height ; i++ )
	{
		for (int j = 0 ; j < width ; j++ )
		{
			grid[i][j] = tempGrid[i][j];
		}
	}
}

bool Grid::checkPoint(int a, int b)
{
   return grid[a][b];
}

bool Grid::changePoint(int a, int b)
{
   grid[a][b] = !grid[a][b];
   return grid[a][b];
}

//Prints the grid into the console.
/*void Grid::printFrame() {
	for (int i = 0; i < height ; i++ )
	{
		for (int j = 0 ; j < width ; j++ )
		{
			if ( grid[i][j] == true )
			{
				cout << "x";
			}
			else
			{
				cout << " ";
			}

			if ( (j + 1) >= width )
			{
				cout << endl;
			}
		}
	}
}
*/

